<template>
<div>
  <el-dialog title="编辑界面" :visible.sync="showFlag" @close="closeDialog" width="40%">
    <el-form ref="editDeviceForm" :model="editDeviceForm" :rules="editDeviceRules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="房间编号" prop="rid">
            <el-input v-model="editDeviceForm.rid" placeholder="请输入房间编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公寓编号" prop="apt_id">
            <el-input v-model="editDeviceForm.apt_id" placeholder="请输入公寓编号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="name">
            <el-input v-model="editDeviceForm.name" placeholder="请输入设备名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备价格" prop="price">
            <el-input v-model="editDeviceForm.price" placeholder="请输入设备价格"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备状态" prop="dstatus">
            <el-select v-model="editDeviceForm.dstatus" placeholder="请选择设备状态" style="width: 100%">
              <el-option label="使用中" value="10"></el-option>
              <el-option label="维护中" value="20"></el-option>
              <el-option label="损坏" value="30"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报修状态" prop="report">
            <el-select v-model="editDeviceForm.report" placeholder="请选择报修状态" style="width: 100%">
              <el-option label="未报修" value="10"></el-option>
              <el-option label="已报修" value="20"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item class="dialog-footer" align="center">
        <el-button type="success" @click="onSave('editDeviceForm')">保 存</el-button>
        <el-button @click="onCancel('editDeviceForm')">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>

<script>
export default {
name: "editDevice",
  data(){
    return{
      editDeviceForm:{
        id:'',
        rid:'',
        apt_id:'',
        name:'',
        price:'',
        dstatus:'',
        report:''
      },
      editDeviceRules: {
        rid:[{required:true,message: '请输入房间编号',trigger: 'blur'}],
        apt_id:[{required:true,message: '请输入公寓编号',trigger: 'blur'}]
      },
      showFlag:false,
    }
  },
  methods: {
    //初始化
    init(row) {
      this.$nextTick(()=>{
        this.row=row
        this.getDeviceDetail(row)
      })
      this.showFlag=true
    },
    //获得本行数据
    getDeviceDetail(row) {
      this.editDeviceForm=row
      this.editDeviceForm.dstatus === 10 ? this.editDeviceForm.dstatus = '使用中': this.editDeviceForm.dstatus === 20 ? this.editDeviceForm.dstatus = '维护中': this.editDeviceForm.dstatus = '损坏'
      this.editDeviceForm.report===10?this.editDeviceForm.report='未报修':this.editDeviceForm.report='已报修'
    },
    //关闭对话框的回调函数
    closeDialog() {
      this.$refs.editDeviceForm.resetFields()
    },
    //保存

    //服务器遇到一个意外的情况，阻止它完成请求。
    onSave() {
      this.$refs.editDeviceForm.validate(async valid=>{
        if(valid){
          console.log('hahha',this.editDeviceForm)
          if(this.editDeviceForm.dstatus === '使用中' || this.editDeviceForm.dstatus === '10'){
            this.editDeviceForm.dstatus= 10
          }else if(this.editDeviceForm.dstatus === '维护中' || this.editDeviceForm.dstatus === '20'){
            this.editDeviceForm.dstatus = 20
          }else{
            this.editDeviceForm.dstatus = 30
          }
          if(this.editDeviceForm.report === '未使用' || this.editDeviceForm.dstatus === '10'){
            this.editDeviceForm.dstatus= 10
          }else{
            this.editDeviceForm.dstatus = 20
          }
          console.log(this.editDeviceForm.dstatus)
          console.log(this.editDeviceForm)
          const {data:res}= await this.$http.post('/rDevice/updateRDeviceById',this.editDeviceForm)
          if(res.code!=200) return this.$message.error('修改设备信息失败！')
          this.$message.success('修改设备信息成功')
        }
      })
      this.$emit('editClickBack')
      this.showFlag = false
    },
    //取消
    onCancel() {
      this.showFlag = false
      this.$refs.editDeviceForm.resetFields()
    }
  }
}
</script>

<style scoped>

</style>